package chpt10;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FirstJFrame extends JFrame {
	private JLabel label;
	private JTextField textField;
	private JButton button;

	public FirstJFrame(String title) {
		super(title); // 复用JFrame构造方法
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		// 为panel设置边框
		panel.setBorder(BorderFactory.createTitledBorder("信息输入"));

		label = new JLabel("姓名：");
		textField = new JTextField(10);

		button = new JButton("显示");
		button.addActionListener(event -> {
			String text = textField.getText();
			JOptionPane.showMessageDialog(FirstJFrame.this, 
					"你好！" + text);
		});

		panel.add(label);
		panel.add(textField);
		panel.add(button);
		add(panel);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			String title = "First JFrame";
			FirstJFrame frame = new FirstJFrame(title);
			frame.pack(); // 自动调节窗体大小以适应其中组件
			frame.setLocationRelativeTo(null); // 将窗体居中显示
			frame.setVisible(true);
		});
	}
}
